package com.clark.crawler.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * description:Map和Bean互转工具类
 *
 * @author tianhua.xie（tianhua.xie@ucarinc.com）
 * @version 1.0
 * @date 2018/12/13 15:04
 */
public class BeanUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(BeanUtils.class);

    /**
     * description: 利用Introspector,PropertyDescriptor将Map转化为Bean
     *
     * @author tianhua.xie（tianhua.xie@ucarinc.com）
     * @date 2018-12-13 15:22:44
     * @param map
     * @param clazz
     * @return java.lang.Object
     **/
    public static Object parseBean(Map<String, Object> map,  Class<?> clazz){

        Object obj ;
        try {
            obj = clazz.getDeclaredConstructor().newInstance();
            parseBean(map,obj);
        } catch (Exception e) {
            LOGGER.error("parseBean Error  ",e);
            throw new RuntimeException(e);
        }
        return obj;
    }

    /**
     * description: 利用Introspector,PropertyDescriptor将Map转化为Bean
     *
     * @author tianhua.xie（tianhua.xie@ucarinc.com）
     * @date 2018-12-13 15:22:44
     * @param map
     * @param o
     * @return java.lang.Object
     **/
    public static Object parseBean(Map<String, Object> map, Object obj){

        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    // 得到property对应的setter方法
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                }
            }
        } catch (Exception e) {
            LOGGER.error("parseBean Error  ",e);
            throw new RuntimeException(e);
        }
        return obj;
    }

    /**
     * description: 利用Introspector和PropertyDescriptor将Bean转化为Map
     *
     * @author tianhua.xie（tianhua.xie@ucarinc.com）
     * @date 2018-12-13 15:12:00
     * @param obj
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public static Map<String, Object> parseMap(Object obj) {

        if(obj == null){
            return null;
        }
        if (obj instanceof Map){
            return (Map<String, Object>) obj;
        }
        Map<String, Object> map = new HashMap<String, Object>(0);
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    map.put(key, value);
                }
            }
        } catch (Exception e) {
            LOGGER.error("parseMap Error  ",e);
            throw new RuntimeException(e);
        }
        return map;
    }

}